package com.jus.player.adapter.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jus.player.R;

import java.util.List;
import java.util.Map;

public class EpisodeAdapter extends RecyclerView.Adapter<EpisodeAdapter.EpisodeViewHolder> {

    private final OnItemEpisodeClickListener listener;
    private final List<Map<String, Object>> episodes;

    public EpisodeAdapter(List<Map<String, Object>> episodes, OnItemEpisodeClickListener listener) {
        this.episodes = episodes;
        this.listener = listener;
    }

    @NonNull
    @Override
    public EpisodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_episode, parent, false);
        return new EpisodeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull EpisodeViewHolder holder, int position) {
        Map<String, Object> map = episodes.get(position);
        if (map == null) {
            return;
        }
        Object unlockObj = map.get("unlock");
        boolean unlock = unlockObj != null && (boolean) unlockObj;
        holder.bind(map.get("episode") + "", unlock);
        //电视剧集选集的点击事件
        holder.itemView.setOnClickListener(v -> {
            // 处理点击事件，如跳转到播放页面
            listener.playSelectEpisode(position, unlock);
        });
    }

    @Override
    public int getItemCount() {
        return episodes.size();
    }

    public static class EpisodeViewHolder extends RecyclerView.ViewHolder {
        // 视图元素，如TextView、ImageView等
        private final View itemView;
        private final ImageView lockIcon;
        private final TextView episodeNumber;

        public EpisodeViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            episodeNumber = itemView.findViewById(R.id.episode_number);
            lockIcon = itemView.findViewById(R.id.lockIcon);
        }

        public void bind(String num, boolean unlock) {
            // 根据是否为收费视频来设置锁图标
            if (!unlock) {
                lockIcon.setVisibility(View.VISIBLE);
            }
            // 设置剧集标题并添加点击事件
            episodeNumber.setText(num);
        }
    }

    // 在Adapter类中定义点击事件回调接口
    public interface OnItemEpisodeClickListener {
        void playSelectEpisode(int position, boolean unlock);
    }

}
